package com.hudongwx.godoffood.activity.myActivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.Community_Activity.DetailsActivity;
import com.hudongwx.godoffood.utils.MyApplication;
import com.hudongwx.godoffood.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_community_msg)
public class CommunityMsgActivity extends AppCompatActivity {
    @ViewInject(R.id.my_community_msg_rl_content)
    private RecyclerView recyclerView;
    @ViewInject(R.id.my_community_msg_null)
    private FrameLayout frameLayout;
    private MyMsgAdapter myMsgAdapter;
    private String TAG="CommunityMsgActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        String jsonData = MyApplication.getJsonData();
        if(null!=jsonData){
            try {
                JSONObject data= new JSONObject(jsonData);
                myMsgAdapter = new MyMsgAdapter(data.getJSONArray("Data"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            recyclerView.setAdapter(myMsgAdapter);
        }

    }
    //把数据写死
    //初始化数据
    private void loadData(){
        RequestParams requestParams = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Message?pageSize=5&timestamp=2016-11-10%2019:37:50&token=091b08ca-a083-4e01-8699-6f833e229aed&signature=193d4fe90fb02b6267ceee0f98b4536fa5355929&pageIndex=1");
        x.http().get(requestParams, new Callback.CommonCallback<MyJsonObject>(){
            @Override
            public void onSuccess(MyJsonObject result) {
                if(null!=result){
                    try {
                        String json =result.toString();
                        MyApplication.setJsonData(json);
                        frameLayout.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.VISIBLE);
                        JSONArray jsData = result.getJSONArray("Data");
                        Log.i(TAG, "onSuccess: ");
                        myMsgAdapter = new MyMsgAdapter(jsData);
                        recyclerView.setAdapter(myMsgAdapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else {
                    recyclerView.setVisibility(View.GONE);
                    frameLayout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i(TAG, "onCancelled: ");
            }

            @Override
            public void onFinished() {
                Log.i(TAG, "onFinished: ");
            }
        });
    }
    class MyMsgAdapter extends RecyclerView.Adapter<MyViewHolder>{
        JSONArray data;

        public MyMsgAdapter(JSONArray data) {
            this.data = data;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.my_community_msg_item, null);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            try {
                JSONObject item = data.getJSONObject(position);
                Log.i(TAG, "onBindViewHolder:  item :    "+item);
                holder.tvItemName.setText(item.getString("UserName"));
                holder.tvItemTime.setText(item.getString("DateTime"));
                holder.tvItemContent.setText(item.getString("Message"));
                if("1".equals(item.getString("Type"))) {
                    holder.tvItemReply.setText("回复我的帖子：");
                }else {
                    holder.tvItemReply.setText("回复我的评论：");
                }
                holder.tvItemCard.setText(item.getString("Coneten"));
                x.image().bind(holder.ivItemIcon,item.getString("UserIcon"));
                holder.llAll.setTag(item.getString("PostId"));
                holder.llAll.setOnClickListener(new ClickListenerItem());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            return data.length();
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemName,tvItemTime,tvItemContent,tvItemReply,tvItemCard;
        ImageView ivItemIcon;
        LinearLayout llAll;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvItemName = (TextView) itemView.findViewById(R.id.my_community_msg_item_name);
            tvItemTime = (TextView) itemView.findViewById(R.id.my_community_msg_item_time);
            tvItemContent = (TextView) itemView.findViewById(R.id.my_community_msg_item_content);
            tvItemReply = (TextView) itemView.findViewById(R.id.my_community_msg_item_reply);
            tvItemCard = (TextView) itemView.findViewById(R.id.my_community_msg_item_card);
            ivItemIcon = (ImageView) itemView.findViewById(R.id.my_community_msg_item_icon);
            llAll = (LinearLayout) itemView.findViewById(R.id.my_community_msg_item_all);
        }
    }
    class ClickListenerItem implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CommunityMsgActivity.this, DetailsActivity.class);
            intent.putExtra("postid",v.getTag()+"");
            startActivity(intent);

        }
    }
    //返回拦截
    @Event(R.id.my_community_msg_iv_back)
    private void back(View view){
        finish();
    }

}
